import {
  request
} from "../../utils/request.js";
import regeneratorRuntime from '../../utils/runtime';

var appInst = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    mainColor: appInst.globalData.mainColor,
    searchResult: [],
    keyword:''

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  TimeId: -1,
  /**
   * 
   * @param {搜索条件改变
   * } e 
   */
  handleInput(e) {
    // 1 获取输入框的值
    const {  value } = e.detail;
    // 2 检测合法性
    if (!value.trim()) {
      this.setData({
        isFocus: false
      })
      // 值不合法
      return;
    }
    // 3 准备发送请求获取数据
    this.setData({keyword:value});
    clearTimeout(this.TimeId);
    this.TimeId = setTimeout(() => {
      this.searchData();
    }, 1000);
  },
  /**
   * 搜索数据
   */
  async searchData(){
    const res = await request({
      url: "/search/simpleSearch?keyword=" + this.data.keyword,
      method: "get"
    });
    if(res.code != 0){
      return;
    }
    this.setData({
      searchResult:res.data
    });
  },

  /**
   * 点击查看
   * 
   * @param {}} e 
   */
  handleShowDetail(e){
    let {obj} = e.currentTarget.dataset;
    let type = obj.type;
    let url = '';
    if( type == 'author'){
      url = '/pages/author/detail/index?authorId=' + obj.id;
    }else if(type == 'famous'){
      url = '/pages/chapter/detail/index?contentType=poetry&chapterId=' + obj.parentId ;
    }
    else if(type == 'book'){
      url = '/pages/book/detail/index?bookId=' + obj.id + "&bookName=" +obj.title;
    }
    else if(type == 'poetry'){
      url = '/pages/chapter/detail/index?contentType=poetry&chapterId=' + obj.id ;
    }
    console.log(url);
    wx.navigateTo({
      url: url
    });
  },
  /** 
   * 热门搜搜快捷查询
   */
  handleQuickSearch(e){
    let {type} = e.currentTarget.dataset;
    wx.navigateTo({
      url:'/pages/chapter/list/index?type=' + type,
    });

  }

})